/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vera.hibernate.dao;

import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import vera.hibernate.entity.Receipts;
import vera.hibernate.util.HibernateUtil;

/**
 *
 * @author vgncr35g
 */
public class ReceiptsDAO extends EntityDAO{
    public ReceiptsDAO()
    {
        
    }
    
    public List<Receipts> getAllReceipts()
    {
        Session s = HibernateUtil.openSession();
        Query q = s.getNamedQuery("getAllReceipts");
        List<Receipts> receipts = q.list();
        s.close();
        return receipts;
    }
    
    public Receipts getReceipt(int recptNum)
    {
        Session s = HibernateUtil.openSession();
        Query q = s.getNamedQuery("getReceipt");
        q.setInteger("recptNum", recptNum);
        Receipts receipt = (Receipts) q.uniqueResult();
        s.close();
        return receipt;    
    }
    
    public List<Receipts> getByClientCd(int clientCd)
    {
        Session s = HibernateUtil.openSession();
        Query q = s.getNamedQuery("getReceiptByClientCd");
        q.setInteger("clientCd", clientCd);
        List<Receipts> receipts = q.list();
        s.close();
        return receipts;
    }
    
    public void deleteReceipt(int recptNum)
    {
        deleteEntity(getReceipt(recptNum));
    }
    
    public void deleteReceiptsByClient(int clientCd)
    {
        List<Receipts> receipts = getByClientCd(clientCd);
        for(Receipts r : receipts)
        {
            deleteEntity(r);
        }
    }
}
